﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using System.Collections.ObjectModel;
using MediaSurface.MediaConsumerServiceReference;
using System.Collections;

namespace MediaSurface
{
    /// <summary>
    /// Interaction logic for SearchUserControl.xaml
    /// </summary>
    public partial class SearchUserControl : UserControl
    {
        private ObservableCollection<mediaConsumerListing> mediaLibraryList2;

        
        
        public SearchUserControl()
        {
            InitializeComponent();
            SearchBorder.Visibility = Visibility.Collapsed;
            hideSearchButton.Visibility = Visibility.Collapsed;
            mediaLibraryList2 = new ObservableCollection<mediaConsumerListing>();
            AddMediaToLibrary();

        }



        public void AddMediaToLibrary() //Intialize the library with all the media file
        {
            MediaConsumerClient client = new MediaConsumerClient();

           
            IList resultList = client.listMediaInPool(client.getMediaPools());

            if (resultList != null)
            {
                foreach (mediaConsumerListing item in resultList)
                {

                    mediaLibraryList2.Add(item);
                                      
                }

                libraryBar2.ItemsSource = mediaLibraryList2;
                
                
            }

            else { }
            
        }

      

      

         private void searchTextBox_TextChanged(object sender, TextChangedEventArgs e) // Update library and listbox according to text change
         {
            
              string textInput;
              string selectedSearchItem = "All";
              int notify = 0;
              object tt = new object();
              tt= searchOptionSurfaceListBox.SelectedItem;

              if (tt != null)
              {
                  selectedSearchItem = searchOptionSurfaceListBox.SelectedItem.ToString();
                  notify = 1;
              }

              textInput = searchTextBox.Text.ToString();


              if (selectedSearchItem == "Microsoft.Surface.Presentation.Controls.SurfaceListBoxItem: Title")
              {

                  surfaceListBox2.Items.Clear();

                  mediaLibraryList2 = new ObservableCollection<mediaConsumerListing>();

                  MediaConsumerClient client = new MediaConsumerClient();
                  if (textInput != "")
                  {
                      libraryBar2.ItemsSource = null;
                      IList resultList = client.searchByTitle(client.getMediaPools(), textInput);
                      if (resultList != null)
                      {

                          foreach (mediaConsumerListing item in resultList)
                          {

                              mediaLibraryList2.Add(item);

                          }

                          libraryBar2.ItemsSource = mediaLibraryList2;
                      }

                      else { }


                  }

              }




              if (selectedSearchItem == "Microsoft.Surface.Presentation.Controls.SurfaceListBoxItem: Friends Media")
              {


                  if (textInput != "")
                  {
                      surfaceListBox2.Items.Clear();
                      mediaLibraryList2 = new ObservableCollection<mediaConsumerListing>();

                      MediaConsumerClient client = new MediaConsumerClient();

                      String[] resultList2 = client.searchPool(textInput);
                      if(resultList2!=null)
                      {
                      for (int i = 0; i < resultList2.Count(); i++)
                      {

                          String dd = resultList2[i];
                          ListBoxItem Listitem = new ListBoxItem { Content = dd };
                          surfaceListBox2.Items.Add(Listitem);
                      }
                      }


                  }
              
              }


              if (selectedSearchItem == "Microsoft.Surface.Presentation.Controls.SurfaceListBoxItem: My Media")
              {
                  string[] myPoolName = new string[1];
                  myPoolName[0] = "bill";

                  mediaLibraryList2 = new ObservableCollection<mediaConsumerListing>();

                  MediaConsumerClient client = new MediaConsumerClient();

                  if (textInput != "")
                  {
                      libraryBar2.ItemsSource = null;
                      IList resultList = client.searchByTitle(myPoolName, textInput);
                      if (resultList != null)
                      {

                          foreach (mediaConsumerListing item in resultList)
                          {

                              mediaLibraryList2.Add(item);

                          }
                          libraryBar2.ItemsSource = mediaLibraryList2;
                      }
                      else { }


                  }

              }

              if (notify == 0)
              
              {

                  if (textInput != "" )
                  {
                      libraryBar2.ItemsSource = null;
                      mediaLibraryList2 = new ObservableCollection<mediaConsumerListing>();

                      MediaConsumerClient client = new MediaConsumerClient();

                      IList resultList = client.searchByLetter(client.getMediaPools(), textInput);
                      if (resultList != null)
                      {

                          foreach (mediaConsumerListing item in resultList)
                          {

                              mediaLibraryList2.Add(item);

                          }
                          libraryBar2.ItemsSource = mediaLibraryList2;
                      }
                      else { }


                  }

              }
            


         }

       
        

         private void surfaceListBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
         {
             String selectedSearchItemInSurfacListBox;


             selectedSearchItemInSurfacListBox = searchOptionSurfaceListBox.SelectedItem.ToString();
           

            

             if (selectedSearchItemInSurfacListBox == "Microsoft.Surface.Presentation.Controls.SurfaceListBoxItem: Friends Media")
             {
                
                     IList selectedSurfacListBox3 = surfaceListBox2.SelectedItems;
                     foreach (System.Windows.Controls.ListBoxItem item in selectedSurfacListBox3)
                     {

                             
                             string poolOwner;
                             poolOwner = item.Content.ToString();
                             string[] getpool = new string[1];
                             getpool[0] = poolOwner;


                           
                             mediaLibraryList2 = new ObservableCollection<mediaConsumerListing>();
                             libraryBar2.ItemsSource = null;
                             MediaConsumerClient client = new MediaConsumerClient();
                             
                             IList resultList = client.listMediaInPool(getpool);
                             

                             if (resultList != null)
                             {
                                 foreach (mediaConsumerListing item2 in resultList)
                                 {

                                     mediaLibraryList2.Add(item2);

                                 }

                                


                                 libraryBar2.ItemsSource = mediaLibraryList2;
                             }
                             else { }

                    }

             }



             if (selectedSearchItemInSurfacListBox == "Microsoft.Surface.Presentation.Controls.SurfaceListBoxItem: Tag")
             {

                 IList selectedSurfacListBox55 = surfaceListBox2.SelectedItems;
                 foreach (System.Windows.Controls.ListBoxItem item in selectedSurfacListBox55)
                 {
                     string tag;
                     tag = item.Content.ToString();
                     string[] getTag = new string[1];
                     getTag[0] = tag;



                     mediaLibraryList2 = new ObservableCollection<mediaConsumerListing>();
                     libraryBar2.ItemsSource = null;
                     MediaConsumerClient client = new MediaConsumerClient();

                     IList resultList = client.searchByTag(client.getMediaPools(),getTag);


                     if (resultList != null)
                     {
                         foreach (mediaConsumerListing item2 in resultList)
                         {
                             
                             mediaLibraryList2.Add(item2);

                         }

                         libraryBar2.ItemsSource = mediaLibraryList2;
                     }
                     else { }

                 }

             }

         




        
             if (selectedSearchItemInSurfacListBox == "Microsoft.Surface.Presentation.Controls.SurfaceListBoxItem: Letter")
             {

                 IList selectedSurfacListBox55 = surfaceListBox2.SelectedItems;
                 foreach (System.Windows.Controls.ListBoxItem item in selectedSurfacListBox55)
                 {
                     string letter;
                     letter = item.Content.ToString();
                     string[] getLetter = new string[1];
                     getLetter[0] = letter;



                     mediaLibraryList2 = new ObservableCollection<mediaConsumerListing>();
                     libraryBar2.ItemsSource = null;
                     MediaConsumerClient client = new MediaConsumerClient();

                     IList resultList = client.searchByLetter(client.getMediaPools(), letter);


                     if (resultList != null)
                     {
                         foreach (mediaConsumerListing item2 in resultList)
                         {

                             mediaLibraryList2.Add(item2);

                         }

                         libraryBar2.ItemsSource = mediaLibraryList2;
                     }
                     else { }

                 }

             }

         }

/**************************************************************************************************************************************/

         private void searchOption_ListBoxChanges(object sender, SelectionChangedEventArgs e)
         {
             string selectedSearchItem;

                        
              selectedSearchItem = searchOptionSurfaceListBox.SelectedItem.ToString(); 

             if (selectedSearchItem == "Microsoft.Surface.Presentation.Controls.SurfaceListBoxItem: All")
             {
                 surfaceListBox2.Items.Clear();

                 mediaLibraryList2 = new ObservableCollection<mediaConsumerListing>();

                 MediaConsumerClient client = new MediaConsumerClient();
                 //libraryBar2.ItemsSource = null;

                 IList resultList3 = client.listMediaInPool(client.getMediaPools());

                 if (resultList3 != null)
                 {
                     foreach (mediaConsumerListing item in resultList3)
                     {

                         mediaLibraryList2.Add(item);
                         string gg = item.thumbnailUrl;



                     }


                     libraryBar2.ItemsSource = mediaLibraryList2;



                 }
             }

             
             if (selectedSearchItem == "Microsoft.Surface.Presentation.Controls.SurfaceListBoxItem: Tag")
             {

                 surfaceListBox2.Items.Clear();

                 mediaLibraryList2 = new ObservableCollection<mediaConsumerListing>();
                
                 MediaConsumerClient client = new MediaConsumerClient();

                 String [] resultList = client.getVideoTags();
                 for (int i = 0; i < resultList.Count(); i++ )
                 {

                     String dd = resultList[i];
                     ListBoxItem Listitem = new ListBoxItem { Content = dd };
                     Listitem.Width = 120;
                     Listitem.Height = 20;
                     surfaceListBox2.Items.Add(Listitem);
                 }



                 
             }

             if (selectedSearchItem == "Microsoft.Surface.Presentation.Controls.SurfaceListBoxItem: Friends Media")
             {
                 surfaceListBox2.Items.Clear();
                 mediaLibraryList2 = new ObservableCollection<mediaConsumerListing>();

                 MediaConsumerClient client = new MediaConsumerClient();

                 String[] resultList2 = client.getMediaPools();
                 for (int i = 0; i < resultList2.Count(); i++)
                 {

                     String dd = resultList2[i];
                     ListBoxItem Listitem = new ListBoxItem { Content = dd };
                     surfaceListBox2.Items.Add(Listitem);
                 }


             }

             if (selectedSearchItem == "Microsoft.Surface.Presentation.Controls.SurfaceListBoxItem: My Media")
             {
                 surfaceListBox2.Items.Clear();

                 
                 string[] getpool = new string[1];
                 getpool[0] = "bill";



                 mediaLibraryList2 = new ObservableCollection<mediaConsumerListing>();
                 libraryBar2.ItemsSource = null;
                 MediaConsumerClient client = new MediaConsumerClient();

                 IList resultList = client.listMediaInPool(getpool);


                 if (resultList != null)
                 {
                     foreach (mediaConsumerListing item2 in resultList)
                     {

                         mediaLibraryList2.Add(item2);

                     }




                     libraryBar2.ItemsSource = mediaLibraryList2;
                 }
                 else { }


             }


             if (selectedSearchItem == "Microsoft.Surface.Presentation.Controls.SurfaceListBoxItem: Letter")
             {
                 surfaceListBox2.Items.Clear();
                

             }

             if (selectedSearchItem == "Microsoft.Surface.Presentation.Controls.SurfaceListBoxItem: Title")
             {
                 surfaceListBox2.Items.Clear();


             }

         }



         private void SurfaceButton_Click(object sender, RoutedEventArgs e)
         {
             searchButton.Visibility = Visibility.Collapsed;
             hideSearchButton.Visibility = Visibility.Visible;
             SearchBorder.Visibility = Visibility.Visible;
         }


         private void Hide_SurfaceButton_Click(object sender, RoutedEventArgs e)
         {
             hideSearchButton.Visibility = Visibility.Collapsed;
             searchButton.Visibility = Visibility.Visible;
             SearchBorder.Visibility = Visibility.Collapsed;

         }
    }
}
